package com.blog.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.blog.entity.User;

public class LoginInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		User user = (User) request.getSession().getAttribute("user");
		if (user==null) {
			String servletPath = request.getServletPath();
			/*System.out.println(servletPath);//
			System.out.println(request.getContextPath());
			System.out.println(request.getServletContext());*/
			String ctxPath = request.getContextPath();
			response.sendRedirect(ctxPath + "/login?servletPath="+servletPath);
			// request.getRequestDispatcher("").forward(request, arg1);
			// TODO 是true停止进入下一个拦截器,还是false
			return true;
		}
		return super.preHandle(request, response, handler);
	}
}
